#import "AppDelegate.h"
#import "IRCTask.h"

@implementation AppDelegate

- (NSString *)userName {
   return [[NSUserDefaults standardUserDefaults] objectForKey:@"Name"];
}

- (PokerTable *)currentTable
{
    return currentTable;
}


- (id)sharedInstanceOfClassName:(NSString *)name
{
    id obj = nil;
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"bundle"];
    if (path) {
        NSBundle *b = [[NSBundle allocWithZone:NULL] initWithPath:path];
        if ((b != nil) && ([b principalClass] !=NULL)) {
                obj = [[b principalClass] sharedInstance];
        } else NSLog(@"Can't Load %@!\n", path);
    } else NSLog(@"Couldn't find %@ bundle!\n",name);
    return obj;
}

- (IBAction)showPreferences:(id)sender {
    [[self sharedInstanceOfClassName:@"Preferences"] showWindow:self];
}

- (void)setCurrentTable:(PokerTable *)table;
{
     currentTable = table;
}

- (void)newTable:(id)sender
{
    currentTable = [[NSDocumentController sharedDocumentController] openUntitledDocumentOfType:@"Poker" display:YES];
    [currentTable addPlayer:[self userName] cash:5000];

}

- (IBAction)goOnVacation:sender {
    [[IRCTask sharedIRCTask] goOnVacation];
}

- (IBAction)returnFromVacation:sender {
    [[IRCTask sharedIRCTask] returnFromVacation];
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    currentTable = [[[NSDocumentController sharedDocumentController] documents] objectAtIndex:0];
    [[IRCTask sharedIRCTask] makeIRCConnection:currentTable];
    [[IRCTask sharedIRCTask] joinTable];
}

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"deadhead" forKey:@"Name"];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dict];
}

- (void)leaveTable:(id)sender {
    [[IRCTask sharedIRCTask] leaveTable];
}

- (void)joinTable:(id)sender {
    [[IRCTask sharedIRCTask] joinTable];
}



@end
